package com.solution.framework.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: gg
 * Date: 14-4-9
 * Time: 下午5:11
 * To change this template use File | Settings | File Templates.
 */
public class TreeUtils {
    public static List<com.solution.framework.util.TreeModel> generateTree(List<com.solution.framework.util.TreeModel> treeModels) {
        ArrayList<com.solution.framework.util.TreeModel> treeModels1 = new ArrayList<com.solution.framework.util.TreeModel>();
        for (com.solution.framework.util.TreeModel treeModel : treeModels) {
            if (treeModel.getPid() == null) {
                //父节点
                treeModels1.add(treeModel);
            }
        }
        treeModels.removeAll(treeModels1);
        TreeUtils.generateTree(treeModels, treeModels1);
        return treeModels1;
        //To change body of created methods use File | Settings | File Templates.
    }

    public static void generateTree(List<com.solution.framework.util.TreeModel> treeModels, ArrayList<com.solution.framework.util.TreeModel> treeModels1) {
        for (com.solution.framework.util.TreeModel treeModel : treeModels1) {
            ArrayList<com.solution.framework.util.TreeModel> treeModels2 = new ArrayList<com.solution.framework.util.TreeModel>();
            for (com.solution.framework.util.TreeModel model : treeModels) {
                if (Long.parseLong(model.getPid().toString()) == Long.parseLong(treeModel.getId().toString())) {
                    treeModels2.add(model);
                }
            }
            if (treeModels2.size() > 0) {
                treeModels.remove(treeModels2);
                treeModel.setChildren(treeModels2);
                TreeUtils.generateTree(treeModels, treeModels2);
            }
        }
        //To change body of created methods use File | Settings | File Templates.
    }

    public static com.solution.framework.util.TreeModel getTreeRoot(String name) {
        com.solution.framework.util.TreeModel root = new com.solution.framework.util.TreeModel();
        root.setPid(null);
        root.setId(0l);
        root.setText(name);
        Map<String, String> map = new HashMap<String, String>();
        map.put("url","");
        root.setAttributes(map);
        return root;
    }

    public static void addTreeRoot(List<com.solution.framework.util.TreeModel> treeModels, String name) {
        com.solution.framework.util.TreeModel root = getTreeRoot(name);
        treeModels.add(root);
        //To change body of created methods use File | Settings | File Templates.
    }

    public static ZTreeModel getZTreeRoot(String name){
        ZTreeModel root = new ZTreeModel();
        root.setPid(null);
        root.setId(0l);
        root.setName(name);
        return root;
    }
}
